package in.muton.edushare.pages.cp.backup.restore;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.behavior.IBehavior;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.dao.SystemBackupDAO;
import com.edushare.common.util.DateTimeUtil;
import com.edushare.common.util.SystemUtils;
import com.edushare.common.vo.SystemBackup;
import com.edushare.common.vo.Universities;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

public class ChooseMethodPanel extends EdusharePanel {

	private static final long serialVersionUID = -1200573214492036240L;
	private static Log log = LogFactory.getLog(ChooseMethodPanel.class);
	private IModel<Universities> univModel;

	public ChooseMethodPanel(String id, IModel<Universities> model, final EduShareBasePage page, final RestorePanel restorePanel) {
		super(id, model, page);
		this.univModel = model;
		add(new Form<Void>("uploadFiles"){

			private static final long serialVersionUID = -2452278894171749966L;

			@Override
			protected void onSubmit() {
				SystemBackup bkp = new SystemBackup();
				bkp.setIsRestore(SystemBackup.IS_RESTORE_TRUE);
				bkp.setBackupTitle("Autobackup creation @ " + DateTimeUtil.formatTimestamp(new Date()));
				new SystemBackupDAO(EduShareSession.get().getSessionORMInterface()).createBackup(bkp);
				EduShareSession.get().getSessionORMInterface().commitAllChanges();
				restorePanel.loadPanel(new UploadFilesPanel(RestorePanel.PLACE_HOLDER_PANEL_ID, new CayenneModel<SystemBackup>(bkp), page, restorePanel));
			}
		});
		
		add(new ListView<IModel<SystemBackup>>("restLV", new PropertyModel<List<IModel<SystemBackup>>>(this, "restoreList")){

			private static final long serialVersionUID = -1800280414786815469L;

			@Override
			protected void populateItem(final ListItem<IModel<SystemBackup>> item) {
				SystemBackup bkp = item.getModelObject().getObject();
				item.add(new Label("restTtl", bkp.getBackupTitle()));
				item.add(new Label("restTimestamp", DateTimeUtil.formatTimestamp(bkp.getCreationTimestamp())));
				item.add(new Link<Void>("restLink"){

					private static final long serialVersionUID = 6725015652322732389L;

					@Override
					public void onClick() {
						log.debug("Link to Backup from server files.");
					}});
				Link<Void> delLink;
				item.add(delLink = new Link<Void>("delLink"){
					private static final long serialVersionUID = -6928189719585363955L;
					@Override
					public void onClick() {
						SystemBackup bkp = item.getModelObject().getObject();
						new SystemBackupDAO(getORMInterface()).delete(bkp);
						getORMInterface().commitAllChanges();
					}});
				delLink.add(new SimpleAttributeModifier("onClick", "return confirm('Delete this restore point?');"));
			}
			
		});
	}
	
	public List<IModel<SystemBackup>> getRestoreList(){
		return new CayenneList<SystemBackup>(new SystemBackupDAO(getORMInterface()).getAllBackups()).getObject();
	}

}
